package com.web.portal.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.web.portal.entity.TransferCredit;
import com.web.portal.service.TransferCreditService;

@Controller

public class CreditTransferController {
@Autowired
TransferCreditService transferCreditService;

public TransferCreditService getTransferCreditService() {
	return transferCreditService;
}

public void setTransferCreditService(TransferCreditService transferCreditService) {
	this.transferCreditService = transferCreditService;
}
@RequestMapping (value="/admin/gettransfercredits.do")
public String getTransferCredits (Map<String, Object> model){
	TransferCredit transferCredits= new TransferCredit();
	List<TransferCredit> listTransferCredit=transferCreditService.getList(transferCredits);
	/*
	 * just put one
	 */
	TransferCredit credit=null;
	if(!listTransferCredit.isEmpty()){
	 credit=listTransferCredit.get(0);
	}
	List<TransferCredit>  listTransferCredits= new ArrayList<TransferCredit>();
	listTransferCredits.add(credit);
	model.put("listTransferCredits", listTransferCredits);
	return "transfer_credit_page";
}
@RequestMapping (value="/admin/gettransfercreditpage.do", method=RequestMethod.GET)
public String getAddPage(Map model){
	model.put("transferCredit",new TransferCredit());
	return "add_transfer_credit_page";
}
@RequestMapping(value="/admin/addtransfercredit.do", method=RequestMethod.POST)
public String addTransferCredit(@Valid @ModelAttribute("transferCredit")final TransferCredit transferCredit,BindingResult bindingResult, Map model){
	if(!bindingResult.hasErrors()){
		transferCreditService.createOrUpdate(transferCredit);
		return "redirect:/admin/gettransfercredits.do";
	}
	return "redirect:/admin/gettransfercredits.do";
}
@RequestMapping (value="/admin/edittransfercredit.do", method=RequestMethod.GET)
public String editTransferCreditPage(@RequestParam ("id") final String id, Map model){
	if(id!=null){
		TransferCredit transferCredit=transferCreditService.getTransferCredit(id);
		model.put("transferCredit", transferCredit);
		return "edit_transfer_credit_page";
	}
	return "redirect:/admin/gettransfercredits.do";
}
@RequestMapping (value="/admin/addeditedtransfercredit.do", method=RequestMethod.POST)
public String addEditedTransferCredit(@Valid @ModelAttribute("transferCredit") final TransferCredit transferCredit, BindingResult bindingResult){
	if(!bindingResult.hasErrors()){
		TransferCredit thisTransferCredit=transferCreditService.getTransferCredit(transferCredit.getId());
		thisTransferCredit.setId(transferCredit.getId());
		thisTransferCredit.setHeader(transferCredit.getHeader());
		thisTransferCredit.setLinkToTransferCreditsPage(transferCredit.getLinkToTransferCreditsPage());
		
		/*
		 * save it
		 */
		transferCreditService.saveOrEdit(thisTransferCredit);
		return "redirect:/admin/gettransfercredits.do";
	}
	return "redirect:/admin/edittransfercredit.do";
}
}

